// pages/login/login.js
const app = getApp()
const $ = app.globalData
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		focus: {
			A: false,
			B: false
		},
		input: {
			A: false,
			B: false
		},
		tips_warn: false,
		tips_primary: false
	},
	bindBlur: function(e) {
		this.setData({
			keyboard: 0,
			["focus." + e.target.id]: false,
			["input." + e.target.id]: !!e.detail.value ? true : false,
			["input." + e.target.id]: e.detail.value
		});
		console.log(this.data.input);
	},
	bindFocus: function(e) {
		if (e.currentTarget.id) {
			wx.pageScrollTo({
				scrollTop: e.currentTarget.id > "B" ? 270 : 140
			});
			this.setData({
				["focus." + e.currentTarget.id]: true
			});
		}
	},
	
	// 登录
	loginbut:function(){
		wx.showLoading({title: '登录请求中'})
		var that = this
		setTimeout(function(){
			var logininfo = that.data.input
			wx.hideLoading();
			wx.showLoading({title: '登录中'})
			wx.request({
				url:$.AdminUrl+"/YApi/dologin",
				data:{
					loginname:logininfo['A'],
					password:logininfo['B']
				},
				success:function(data){
					wx.hideLoading();
					if(data.data.code){
						that.setData({tips_primary:true})
						setTimeout(function(){that.setData({tips_primary:false})}, 3000);
						wx.setStorage({
							key:"userinfo",
							data:data.data.obj,
							success:function(){
								$.userinfo = data.data.obj
								wx.navigateBack({ changed: true });
							}
						})
					}else{
						that.setData({tips_warn:true})
						setTimeout(function(){that.setData({tips_warn:false})}, 3000);
					}
					console.log(data)
				}
			})
		},1000)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// 判断是否已经登陆
		wx.getStorage({
			key:"userinfo",
			success:function(data){
				console.log(data)
				wx.switchTab({
					url:"../my/my"
				})
			}
		})
	},
	

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

// 分享转发
	onShareAppMessage: function() {
		var userinfo = wx.getStorageSync('userinfo')
		return {
			title: '优悦购生活管家',
			path: '/pages/login/login?userid='+userinfo.loginname,
			success: (res) => {
				console.log("转发成功", res);
			},
			fail: (res) => {
				console.log("转发失败", res);
			}
		}
	}
})
